﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TTTTBHYT.BUS;
using TTTTBHYT.VO;
using C1.Win.C1FlexGrid;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;

namespace TTTTBHYT
{
    public partial class frmSearch29BHYT : frmParent 
    {
        private Mau29BUS Mau29BUS;
        private List<MAU29VO> lstMau29VO;
        public frmSearch29BHYT()
        {
            InitializeComponent();
            Mau29BUS = new Mau29BUS();
        }


        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            string condition = "";
            int intTienThanhToan = 0, intNbThanhToan = 0, intBHXHThanhToan = 0;

            if (txtKCB.Text != "")
            {
                condition = condition + " and SOKCB like N'%" + txtKCB.Text + "%'";
            }
            if (txtHoten.Text != "")
            {
                condition = condition + " and HOTEN like N'%" + txtHoten.Text + "%'";
            }
            if (txtMaBV.Text != "")
            {
                condition = condition + " and MA_BV = N'" + txtMaBV.Text + "'";
            }
            if (txtMaBVDT.Text != "")
            {
                condition = condition + " and MA_BV_DT = N'" + txtMaBVDT.Text + "'";
            }
            if (cboLoaiDieuTri.SelectedIndex != -1)
            {
                condition = condition + " and LOAIDT = " + cboLoaiDieuTri.SelectedIndex + "";
            }
            if (cboHangBV.SelectedIndex != -1)
            {
                condition = condition + " and HANGBV = " + cboHangBV.SelectedIndex + "";
            }
            if (cboKetLuan.SelectedIndex != -1)
            {
                condition = condition + " and Ketluan = " + cboKetLuan.SelectedIndex + "";
            }
            if (txtQuy.Text != "")
            {
                condition = condition + " and Quy = " + txtQuy.Text + "";
            }
            if (cboThanhtoan.SelectedIndex != -1)
            {
                condition = condition + " and THANHTOAN = " + cboThanhtoan.SelectedIndex + "";
            }
            if (dtTuNgay.Checked == true)
            {
                condition = condition + " and NGAYCT >= '" + dtTuNgay.Value.Month.ToString() + "/" + dtTuNgay.Value.Day.ToString() + "/" + dtTuNgay.Value.Year.ToString() + "'";
            }
            if (dtDenNgay.Checked == true)
            {
                condition = condition + " and NGAYCT <= '" + dtDenNgay.Value.Month.ToString() + "/" + dtDenNgay.Value.Day.ToString() + "/" + dtDenNgay.Value.Year.ToString() + "'";
            }
            if (dtTuNgaytt.Checked == true)
            {
                condition = condition + " and NGAYTT >= '" + dtTuNgaytt.Value.Month.ToString() + "/" + dtTuNgaytt.Value.Day.ToString() + "/" + dtTuNgaytt.Value.Year.ToString() + "'";
            }
            if (dtDenNgaytt.Checked == true)
            {
                condition = condition + " and NGAYTT <= '" + dtDenNgaytt.Value.Month.ToString() + "/" + dtDenNgaytt.Value.Day.ToString() + "/" + dtDenNgaytt.Value.Year.ToString() + "'";
            }
            if ( chkNDX.Checked == true)
            {
                condition = condition + " and BHXHNDX <> 0";
            }
            switch (cboNhom.SelectedIndex)
            {
                case 0:
                    condition = condition + "  and MADT in ('DN','HX', 'CH', 'NN', 'TK', 'HC', 'XK', 'CA', 'TN', 'HD') ";
                    break;
                case 1:
                    condition = condition + "  and MADT in ('HT','BT', 'MS', 'XB', 'XN', 'CC', 'CK', 'CB', 'KC', 'TC','HG', 'TQ','TA', 'TY', 'NO', 'TB') ";
                    break;
                case 2:
                    condition = condition + "  and MADT in ('HN','CN') ";
                    break;
                case 3:
                    condition = condition + "  and MADT in ('TE') ";
                    break;
                case 4:
                    condition = condition + "  and MADT in ('LS','HS') ";
                    break;
                case 5:
                    condition = condition + "  and MADT in ('GD','TL','XV') ";
                    break;
                case 6:
                    condition = condition + "  and MADT = '' ";
                    break;

            }
            if (cboLoai.SelectedIndex == 0)
            {
                condition = condition + "   and SOKCB not like '%GD%' "; 
            }
            else if (cboLoai.SelectedIndex == 1)
            {
                condition = condition + "   and SOKCB like '%GD%' "; 
            }

            fa.Rows.Count = 1;
            if (condition == "") { return; }

            lstMau29VO = Mau29BUS.GetListMau29(condition);
            int i = 1;

            foreach (MAU29VO Mau29VO in lstMau29VO)
            {

                fa.Rows.Count = fa.Rows.Count + 1;
                fa.Rows[i]["STT"] = i;
                fa.Rows[i]["KEYSL"] = Mau29VO.KeySL;
                fa.Rows[i]["SoKCB"] = Mau29VO.SoKCB;
                fa.Rows[i]["HoTen"] = Mau29VO.HoTen;
                fa.Rows[i]["NgaySinh"] = Mau29VO.NgaySinh;
                fa.Rows[i]["GioiTinh"] = Mau29VO.GioiTinh;
                fa.Rows[i]["Mabv"] = Mau29VO.MaBV;
                fa.Rows[i]["tenbvKCB"] = Mau29VO.TenBV;
                fa.Rows[i]["MA_BV_DT"] = Mau29VO.MaBVDT;
                fa.Rows[i]["TENBVDT"] = Mau29VO.TenBVDT;
                fa.Rows[i]["MADT"] = Mau29VO.MaDT;
                fa.Rows[i]["chuandoan"] = Mau29VO.ChuanDoan;
                if (Mau29VO.LoaiDT == 0)
                {
                    fa.Rows[i]["LOAIDT"] = "Ngoại trú";
                }
                else if (Mau29VO.LoaiDT == 1)
                {
                    fa.Rows[i]["LOAIDT"] = "Nội trú";
                }
                switch (Mau29VO.HangBV)
                {
                    case 0:
                        fa.Rows[i]["HANGBV"] = "Đặc biệt/I";
                        break;
                    case 1:
                        fa.Rows[i]["HANGBV"] = "Hạng II";
                        break;
                    case 2:
                        fa.Rows[i]["HANGBV"] = "Hạng III";
                        break;
                    case 3:
                        fa.Rows[i]["HANGBV"] = "Hạng IV";
                        break;
                    default:
                        break;

                }
                switch (Mau29VO.KetLuan)
                {
                    case 0:
                        fa.Rows[i]["KetLuan"] = "Không đúng quy định";
                        break;
                    case 1:
                        fa.Rows[i]["KetLuan"] = "Đúng quy định";
                        break;
                    case 2:
                        fa.Rows[i]["KetLuan"] = "KCB Tự chọn";
                        break;
                    case 3:
                        fa.Rows[i]["KetLuan"] = "Nhập Ngày";
                        break;


                }
                fa.Rows[i]["Quy"] = Mau29VO.Quy;
                fa.Rows[i]["ThanhToan"] = Mau29VO.TienThanhToan;
                fa.Rows[i]["NBTritra"] = Mau29VO.NbThanhToan;
                fa.Rows[i]["BHXHTT"] = Mau29VO.BhxhThanhToan;
                fa.Rows[i]["BHXHNDX"] = Mau29VO.BhxhNDX;
                fa.Rows[i]["NGAY"] = Mau29VO.NgayCT;
                intTienThanhToan = intTienThanhToan + Mau29VO.TienThanhToan;
                intNbThanhToan = intNbThanhToan + Mau29VO.NbThanhToan;
                intBHXHThanhToan = intBHXHThanhToan + Mau29VO.BhxhThanhToan;

                if (Mau29VO.ThanhToan == 1)
                {
                    C1.Win.C1FlexGrid.CellStyle rs = this.fa.Styles.Add("rowcolor");
                    rs.ForeColor = Color.Blue;
                    fa.Rows[i].Style = rs;
                }
                i++;
            }
            fa.Rows.Count = fa.Rows.Count + 1;
            fa.Rows[i]["ThanhToan"] = intTienThanhToan;
            fa.Rows[i]["NBTritra"] = intNbThanhToan;
            fa.Rows[i]["BHXHTT"] = intBHXHThanhToan;
            

            C1.Win.C1FlexGrid.CellStyle rs1 = this.fa.Styles.Add("rowcolor1");
            rs1.ForeColor = Color.Red;
            fa.Rows[i].Style = rs1;

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            txtKCB.Text = "";
            txtHoten.Text = "";
            txtKCB.Text = "";
            txtMaBV.Text = "";
            txtMaBVDT.Text = "";
            txtQuy.Text = "";
            cboHangBV.SelectedIndex = -1;
            cboKetLuan.SelectedIndex = -1;
            cboLoaiDieuTri.SelectedIndex = -1;
            cboThanhtoan.SelectedIndex = -1;
            txtKCB.Focus();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fa_DoubleClick(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            frm29BHYT childForm = GetForm("frm29BHYT");
            if (childForm == null && fa.Rows[fa.Selection.TopRow]["KEYSL"].ToString() != "")
            {
                 
                childForm = new frm29BHYT();
                childForm.Mau29VO = lstMau29VO.Find(
                delegate(MAU29VO Mau29VO)
                {
                    return Mau29VO.KeySL == fa.Rows[fa.Selection.TopRow]["KEYSL"].ToString();
                          
                }
                );
                IEnumerable<MAU29VO> items = from n in lstMau29VO
                                            where n.HoTen.Contains("BÙI") 
                                            select n;

                foreach (var item in items)
                    Console.WriteLine(item);

                childForm.MdiParent = this.MdiParent;
                childForm.WindowState = FormWindowState.Maximized;
                childForm.Show();
                childForm.btnSua_Click(null, null);
                childForm.SetValueToControl();
                childForm.SetValueToFa1();
                
            }
            else
            {
                childForm.Activate();
            }
            Cursor.Current = Cursors.Default;
        }
        private frm29BHYT GetForm(String FormName)
        {
            foreach (frm29BHYT f in this.FindForm().MdiChildren)
            {
                if (f.Name == FormName)
                    return f;
            }
            return null;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            DataRow newRow = null;
            string quy="";
            Dataset.dsMau29 dsMAU29 = new Dataset.dsMau29();
            for (int i = 1; i < fa.Rows.Count-1; i++)
            {
                newRow = dsMAU29.Tables[1].NewRow();
                newRow["Hoten"] = fa.Rows[i]["HoTen"];
                newRow["SoKCB"] = fa.Rows[i]["SoKCB"];
                newRow["Ma_BV_DT"] = fa.Rows[i]["Mabv"];
                newRow["TEN_BV_DT"] = fa.Rows[i]["TENBVDT"];
                newRow["TIENTHANHTOAN"] = fa.Rows[i]["ThanhToan"];
                if (fa.Rows[i]["LOAIDT"].ToString() == "Ngoại trú")
                {
                    newRow["BHXHTHANHTOANNGOAITRU"] = fa.Rows[i]["BHXHTT"];
                    newRow["BHXHNGOAITRUNDX"] = fa.Rows[i]["BHXHNDX"];
                }
                else if (fa.Rows[i]["LOAIDT"].ToString() == "Nội trú")
                {
                    newRow["BHXHTHANHTOANNOITRU"] = fa.Rows[i]["BHXHTT"];
                    newRow["BHXHNOITRUNDX"] = fa.Rows[i]["BHXHNDX"];
                }
               
               
                switch (fa.Rows[i]["HANGBV"].ToString())
                {
                    case "Đặc biệt/I":
                         newRow["TENHANGBV"] = "I. Tuyến Trung Ương";
                         newRow["HANGBV"] = 1;
                         break;
                    case "Hạng II":
                         newRow["TENHANGBV"] = "II. Tuyến tỉnh";
                         newRow["HANGBV"] = 2;
                         break;
                    case "Hạng III":
                         newRow["TENHANGBV"] = "III.Tuyến huyện";
                         newRow["HANGBV"] = 3;
                         break;
                    case "Hạng IV":
                         newRow["TENHANGBV"] = "III.Tuyến xã";
                         newRow["HANGBV"] = 4;
                         break;

                }

                //PHAN NHOM BHYT
                switch (fa.Rows[i]["MADT"].ToString())
                {
                    case "DN":
                    case "HX":
                    case "CH":
                    case "NN":
                    case "TK":
                    case "HC":
                    case "XK":
                    case "CA":
                    case "TN":
                    case "HD":
                        newRow["NHOM"] = "NHÓM I";
                        break;
                    case "HT":
                    case "BT":
                    case "MS":
                    case "XB":
                    case "XN":
                    case "CC":
                    case "CK":
                    case "CB":
                    case "KC":
                    case "TC":
                    case "HG":
                    case "TQ":
                    case "TA":
                    case "TY":
                    case "NO":
                    case "TB":
                        newRow["NHOM"] = "NHÓM II";
                        break;
                    case "HN":
                    case "CN":
                        newRow["NHOM"] = "NHÓM III";
                        break;
                    case "TE":
                        newRow["NHOM"] = "NHÓM IV";
                        break;
                    case "LS":
                    case "HS":
                        newRow["NHOM"] = "NHÓM V";
                        break;
                    case "GD":
                    case "TL":
                    case "XV":
                        newRow["NHOM"] = "NHÓM VI";
                        break;
                }

                dsMAU29.Tables[1].Rows.Add(newRow);
            }
            //quy
            switch (txtQuy.Text.Substring(0,1))
            {
                case "1":
                    quy = "I NĂM " + txtQuy.Text.Substring(1, 4);
                    break;
                case "2":
                    quy = "II NĂM " + txtQuy.Text.Substring(1, 4);
                    break;
                case "3":
                    quy = "III NĂM " + txtQuy.Text.Substring(1, 4);
                    break;
                case "4":
                    quy = "IV NĂM " + txtQuy.Text.Substring(1, 4);
                    break;

            }
            Reports f = new Reports();
            ReportDocument cr = new ReportDocument();
            cr.Load("rptMau09.rpt");
            cr.SetDataSource(dsMAU29);
            foreach (CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition thisFormulaField in cr.DataDefinition.FormulaFields)
            {
                switch (thisFormulaField.Name)
                {
                    case "Giamdoc":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Giamdoc + "  '";
                        break;
                    case "Giamdinh":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Giamdinh + "  '";
                        break;
                    case "Ketoan":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Ketoantruong + "'";
                        break;
                    case "Quy":
                        thisFormulaField.Text = "'" + quy + "'";
                        break;
                }
            }
            f.crpView.ReportSource = cr;
            f.Show();
        }

        private void btn09bhyt_Click(object sender, EventArgs e)
        {
            DataRow newRow = null;
            Dataset.dsMau29 dsMAU29 = new Dataset.dsMau29();
            for (int i = 1; i < fa.Rows.Count - 1; i++)
            {
                newRow = dsMAU29.Tables[1].NewRow();
                newRow["Hoten"] = fa.Rows[i]["HoTen"];
                newRow["SoKCB"] = fa.Rows[i]["SoKCB"];
                newRow["Ma_BV_DT"] = fa.Rows[i]["Mabv"];
                newRow["TEN_BV_DT"] = fa.Rows[i]["TENBVkcb"];

                newRow["TIENTHANHTOAN"] = fa.Rows[i]["ThanhToan"];
                newRow["BHXHNDX"] = fa.Rows[i]["BHXHNDX"];
                if (fa.Rows[i]["LOAIDT"].ToString() == "Ngoại trú")
                {
                    newRow["BHXHTHANHTOANNGOAITRU"] = fa.Rows[i]["BHXHTT"];
                    newRow["BHXHNGOAITRUNDX"] = fa.Rows[i]["BHXHNDX"];
                }
                else if (fa.Rows[i]["LOAIDT"].ToString() == "Nội trú")
                {
                    newRow["BHXHTHANHTOANNOITRU"] = fa.Rows[i]["BHXHTT"];
                    newRow["BHXHNOITRUNDX"] = fa.Rows[i]["BHXHNDX"];
                }
                newRow["HANGBV"] = fa.Rows[i]["HANGBV"];

                switch (newRow["Ma_BV_DT"].ToString().Substring(2))
                {
                    case "001":
                        newRow["TENHANGBV"] = "I. Tuyến Trung Ương";
                        break;
                    case "012":
                        newRow["TENHANGBV"] = "II. Tuyến tỉnh";
                        break;
                    default:   
                        newRow["TENHANGBV"] = "III.Tuyến huyện";
                        break;
                    
                }

                dsMAU29.Tables[1].Rows.Add(newRow);
            }
            Reports f = new Reports();
            ReportDocument cr = new ReportDocument();
            cr.Load("rptMau09TH.rpt");
            cr.SetDataSource(dsMAU29);
            foreach (CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition thisFormulaField in cr.DataDefinition.FormulaFields)
            {
                switch (thisFormulaField.Name)
                {
                    case "Giamdoc":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Giamdoc + "  '";
                        break;
                    case "Giamdinh":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Giamdinh + "  '";
                        break;
                    case "Ketoan":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Ketoantruong + "'";
                        break;

                }
            }
            f.crpView.ReportSource = cr;
            f.Show();
        }

        private void btnNDT_Click(object sender, EventArgs e)
        {
            DataRow newRow = null;
            Dataset.dsMau29 dsMAU29 = new Dataset.dsMau29();
            for (int i = 1; i < fa.Rows.Count - 1; i++)
            {
                newRow = dsMAU29.Tables[1].NewRow();
                newRow["Hoten"] = fa.Rows[i]["HoTen"];
                newRow["SoKCB"] = fa.Rows[i]["SoKCB"];
                newRow["TEN_BV_DT"] = fa.Rows[i]["TENBVDT"];
                newRow["Ma_BV_DT"] = fa.Rows[i]["Mabv"];
              
                newRow["TIENTHANHTOAN"] = fa.Rows[i]["ThanhToan"];
                newRow["BHXHNDX"] = fa.Rows[i]["BHXHNDX"];
                if (fa.Rows[i]["LOAIDT"].ToString() == "Ngoại trú")
                {
                    newRow["BHXHTHANHTOANNGOAITRU"] = fa.Rows[i]["BHXHTT"];
                }
                else if (fa.Rows[i]["LOAIDT"].ToString() == "Nội trú")
                {
                    newRow["BHXHTHANHTOANNOITRU"] = fa.Rows[i]["BHXHTT"]; ;
                }
                newRow["HANGBV"] = fa.Rows[i]["HANGBV"];

                switch (fa.Rows[i]["HANGBV"].ToString())
                {
                    case "Đặc biệt/I":
                        newRow["TENHANGBV"] = "I. Tuyến Trung Ương";
                        break;
                    case "Hạng II":
                        newRow["TENHANGBV"] = "II. Tuyến tỉnh";
                        break;
                    case "Hạng III":
                        newRow["TENHANGBV"] = "III.Tuyến huyện";
                        break;
                    case "Hạng IV":
                        newRow["TENHANGBV"] = "III.Tuyến xã";
                        break;

                }
                              

                dsMAU29.Tables[1].Rows.Add(newRow);
            }
            Reports f = new Reports();
            ReportDocument cr = new ReportDocument();
            cr.Load("rptMau09TH.rpt");
            cr.SetDataSource(dsMAU29);
            foreach (CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition thisFormulaField in cr.DataDefinition.FormulaFields)
            {
                switch (thisFormulaField.Name)
                {
                    case "Giamdoc":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Giamdoc + "  '";
                        break;
                    case "Giamdinh":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Giamdinh + "  '";
                        break;
                    case "Ketoan":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Ketoantruong + "'";
                        break;

                }
            }
            f.crpView.ReportSource = cr;
            f.Show();
        }

      

        
    }
}
